package com.chushouya.manager.service.api.impl;


import com.chushouya.manager.dao.entity.ArticleEntity;
import com.chushouya.manager.dao.repository.ArticleRepository;
import com.chushouya.manager.dto.admin.article.ArticleApiDetailDTO;
import com.chushouya.manager.dto.admin.article.ArticleApiListDTO;
import com.chushouya.manager.dto.admin.article.ArticleApiQuery;
import com.chushouya.manager.dto.admin.article.ArticleQuery;
import com.chushouya.manager.service.api.ArticleApiService;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Validate;
import com.general.framework.core.schema.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


@Service
public class ArticleApiServiceImpl implements ArticleApiService {

    @Resource
    private ArticleRepository articleRepository;


    @Override
    public PageResult<ArticleApiListDTO> selectPage(ArticleApiQuery query) {
        PageResult<ArticleEntity> page = articleRepository.selectPage(Beans.copy(query, ArticleQuery.class));
        PageResult<ArticleApiListDTO> pageResult = PageResult.of(page, ArticleApiListDTO.class);
        return pageResult;
    }

    @Override
    public ArticleApiDetailDTO detail(Long articleId) {

        Validate.isNull(articleId, "articleId不能为空");
        ArticleEntity article = articleRepository.selectByPrimaryKey(articleId);
        Validate.isNull(article, "文章不存在");
        ArticleApiDetailDTO articleResult = Beans.copy(article, ArticleApiDetailDTO.class);
        return articleResult;
    }

    @Override
    public List<ArticleApiListDTO> selectList(ArticleApiQuery query) {
        List<ArticleEntity> articleList = articleRepository.selectList(Beans.copy(query, ArticleQuery.class));
        if (articleList == null || articleList.isEmpty()) {
            return Collections.emptyList();
        }
        return Beans.copyList(articleList, ArticleApiListDTO.class);
    }


}
